/**
 * @Date 2023-02-25 15:45:10
 * @Remark
 */

// lib
import _ from "lodash";
import numbro from "numbro";
import NP from "number-precision";
// config
// script & methods & public
// biz
// store
// service
// interface
// 其它

// 格式化
function formatNumberToText(
  price: number | undefined | null,
  mantissa?: number,
) {
  const text = numbro(price || 0).format({
    mantissa: _.isNumber(mantissa) ? mantissa : 2,
  });
  return text;
}

function formatPrice(price: number | undefined | null) {
  const text = numbro(price || 0).format({
    mantissa: 2,
  });
  return `￥${text}`;
}

function formatPercent(percent: any) {
  if (!percent) {
    return "0";
  }
  const pNum = percent * 1;
  if (pNum === 1) {
    return "100%";
  }
  return `${(pNum * 100).toFixed(2)}%`;
}

export function createFrontIndex(index: number, page: number, size: number) {
  const p = NP.minus(page, 1);
  const i = NP.plus(index, 1);
  const add = NP.times(p, size);
  return NP.plus(i, add);
}

const numberUtils = {
  formatNumberToText,
  formatPrice,
  formatPercent,
  createFrontIndex,
};

export default numberUtils;
